﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System.Linq;
using Sovacode.DemoApp.Commands;
using Sovacode.DemoApp.Model;

namespace Sovacode.DemoApp.ViewModel.BasicViewModel
{
    /// <summary>
    /// Base Validation View Model with Create/Remove/Clear Selected Data Object
    /// </summary>
    public abstract class BaseValidationViewModel : ChildsHolderViewModel
    {
        #region Private fields

        /// <summary>
        /// The name
        /// </summary>
        private string _name = string.Empty;

        /// <summary>
        /// The special property
        /// </summary>
        private string _specialProperty = string.Empty;

        #endregion

        #region Public properties

        /// <summary>
        /// Name of data object
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;

                OnPropertyChanged(() => Name);
            }
        }

        /// <summary>
        /// Gets or sets the special property.
        /// </summary>
        /// <value>The special property.</value>
        public string SpecialProperty
        {
            get { return _specialProperty; }
            set
            {
                _specialProperty = value;
                OnPropertyChanged((() => SpecialProperty));
            }
        }

        #endregion

        #region Override

        /// <summary>
        /// Registration common commands
        /// </summary>
        protected override void RegisterCommands()
        {
            base.RegisterCommands();
            
            /* Save new Data Object */
            RegisterCommand(Const.CreateNewDataObject, o => DataObjects.Add(new SimpleDataObject { Name = this.Name, SpecialProperty = this.SpecialProperty }), p => IsValid && DataObjects.All(d => string.CompareOrdinal(d.Name, Name) != 0));
            
            /* Save old (Selected) Data Object */
            RegisterCommand(Const.SaveOldDataObject, o => SelectedDataObject.Name = this.Name, p => IsValid && SelectedDataObject != null);
            
            /* Remove Selected Data Object */
            RegisterCommand(Const.RemoveOldDataObject, o => DataObjects.Remove(SelectedDataObject), p =>
                SelectedDataObject != null &&
                DataObjects.Count > 0 &&
                DataObjects.Contains(SelectedDataObject));

            /* Clear All Data Objects */
            RegisterCommand(Const.ClearAll, o => { SelectedDataObject = null; DataObjects.Clear(); });
        }

        /// <summary>
        /// Update View after Selected DataObject change
        /// </summary>
        protected override void UpdateView()
        {
            if (SelectedDataObject == null)
            {
                Name = string.Empty;

                return;
            }

            Name = SelectedDataObject.Name;
            SpecialProperty = SelectedDataObject.SpecialProperty;
        }

        #endregion
    }
}